package com.test.ws.s1.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.ws.s1.bean.ClassIf;
import com.test.ws.s1.service.ClassIfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@Controller
public class ClassIfController {
    @Autowired
    private ClassIfService classIfService;

    /// 获取所有分类信息
    @RequestMapping("selectClassIf")
    public String selectClassIf(ClassIf classIf, HttpServletRequest request, String classif_de,
                                @RequestParam(defaultValue = "1") int pageNum,
                                @RequestParam(defaultValue = "3") int pageSize) {
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize);
        System.out.println("================"+classif_de);


        ArrayList<ClassIf> classIfs = classIfService.selectClassIf(classIf);
        PageInfo pageInfo = new PageInfo(classIfs);
        System.out.println(classIfs+"====================");
        request.setAttribute("classIfs", classIfs);
        request.setAttribute("pageInfo",pageInfo);

        return "houtai/classif";
    }


    /// 获取所有分类信息
    @RequestMapping("selectJinYongClassIf")
    public String selectJinYongClassIf(ClassIf classIf, HttpServletRequest request,
                                @RequestParam(defaultValue = "1") int pageNum,
                                @RequestParam(defaultValue = "3") int pageSize) {
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize);
        ArrayList<ClassIf> classIfs = classIfService.selectJinYongClassIf(classIf);
        PageInfo pageInfo = new PageInfo(classIfs);
        System.out.println(classIfs+"====================");
        request.getSession().setAttribute("classifJY", classIfs);
        request.setAttribute("pageInfo",pageInfo);

        return "houtai/classifJY";
    }

    /// 进入分类页面
    @RequestMapping("classIfAdd")
    public String classIfAdd(HttpServletRequest request) {
        return "houtai/classifZj";
    }


    /// 添加分类信息
    @RequestMapping("insertClassIf")
    public String insertClassIf(ClassIf classIf, HttpServletRequest request) {
        int i = classIfService.insertClassIf(classIf);
        System.out.println(i+"=============="+classIf);
        return "redirect:/selectClassIf";
    }


    /// 删除分类信息
    @RequestMapping("deleteClassIf")
    public String deleteClassIf(ClassIf classIf, HttpServletRequest request) {
        String id = request.getParameter("id");
        classIf.setClassif_id(Integer.parseInt(id));
        int i = classIfService.deleteClassIf(classIf);
        System.out.println(i+"=============="+classIf);
        return "redirect:/selectClassIf";
    }

    /// 回显
    @RequestMapping("selectOneClassIf")
    public String selectOneClassIf(ClassIf classIf, HttpServletRequest request) {
        String id = request.getParameter("id");
        classIf.setClassif_id(Integer.parseInt(id));
        ClassIf classIfs = classIfService.selectOneClassIf(classIf);
        request.setAttribute("hxsj", classIfs);
        return "houtai/classifXiuGai";
    }


    /// 修改分类
    @RequestMapping("updateClassIf")
    public String updateClassIf(ClassIf classIf, HttpServletRequest request) {
        int i = classIfService.updateClassIf(classIf);
        System.out.println(i+"=============="+classIf);
        return "redirect:/selectClassIf";
    }

}
